package com.chinairi.mall.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author bianyong
 * @since 2019/9/12 15:42
 */
public class DateUtils {
    public static Date getDate (String partten, String date)  {
        SimpleDateFormat sdf = new SimpleDateFormat(partten);
        if(date == null||"".equals(date)){
            return null;
        }else{
            try {
                return sdf.parse(date);
            }catch (ParseException e){
                return null;
            }

        }
    }
    public static String getDate (String partten, Date date)  {
        SimpleDateFormat sdf = new SimpleDateFormat(partten);
        if(date == null){
            return null;
        }else{
            return sdf.format(date);
        }
    }
    /**
     * 计算两个日期之间相差的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetween(Date date1, Date date2){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days=(time1-time2)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
    /**
     * 计算两个日期之间相差的分钟数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentMinutes(Date date1, Date date2){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_minutes=(time1-time2)/(1000*60);

        return Integer.parseInt(String.valueOf(between_minutes));
    }

    public static boolean comPareTime(Date date, Date restStartTime, Date endTime) {
        if (date.compareTo(restStartTime)>0&&date.compareTo(endTime)<0){
            return true;
        }else {
            return  false;
        }
    }

    /**
     * 计算减去多少天后的日期
     * @param assemblyDate
     * @param purchaseCycle
     * @return
     */
    public static Date substractDate(Date assemblyDate, Integer purchaseCycle) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(assemblyDate);
        calendar.add(Calendar.DAY_OF_MONTH,-purchaseCycle);
        return calendar.getTime();
    }
    /**
     * 获取指定日期所在月的第一天和最后一天,用下划线连接
     * @param dataStr
     * @return
     * @throws ParseException
     */
    public static String getFirstAndLastOfMonth(String dataStr, String dateFormat, String resultDateFormat)  {
        try{
            //获取当前月第一天：
            Calendar c = Calendar.getInstance();
            c.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
            c.add(Calendar.MONTH, 0);
            //设置为1号,当前日期既为本月第一天
            c.set(Calendar.DAY_OF_MONTH,1);
            String first = new SimpleDateFormat(resultDateFormat).format(c.getTime());
            System.out.println("===============first:"+first);

            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = new SimpleDateFormat(resultDateFormat).format(ca.getTime());
            System.out.println("===============last:"+last);
            return first+"_"+last;
        }catch (ParseException e){
            return null;
        }
    }

    /**
     * 每周的第一天和最后一天
     * @param dataStr
     * @param dateFormat
     * @param resultDateFormat
     * @return
     * @throws ParseException
     */
    public static String getFirstAndLastOfWeek(String dataStr, String dateFormat, String resultDateFormat)  {
        try{
            Calendar cal = Calendar.getInstance();
            cal.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
            int d = 0;
            if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
                d = -6;
            } else {
                d = 2 - cal.get(Calendar.DAY_OF_WEEK);
            }
            cal.add(Calendar.DAY_OF_WEEK, d);
            // 所在周开始日期
            String data1 = new SimpleDateFormat(resultDateFormat).format(cal.getTime());
            cal.add(Calendar.DAY_OF_WEEK, 6);
            // 所在周结束日期
            String data2 = new SimpleDateFormat(resultDateFormat).format(cal.getTime());
            return data1 + "_" + data2;
        }catch (ParseException e){
            return null;
        }
    }

    /**
     * 获取指定日期所在季度的第一天和最后一天,用下划线连接
     * @param dataStr
     * @param dateFormat
     * @param resultDateFormat
     * @return
     * @throws ParseException
     */
    public static String getFirstAndLastOfQuarter(String dataStr, String dateFormat, String resultDateFormat) throws ParseException {
        //获取当前季度的开始时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
        int month = calendar.get(Calendar.MONTH);
        int quarter = month / 3 + 1;
        int startMonth = 1;
        if (quarter == 2){
            startMonth = 4;
        }else if(quarter == 3){
            startMonth = 7;
        }else if(quarter == 4){
            startMonth = 10;
        }
        calendar.set(Calendar.MONTH,startMonth - 1);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        String date1=new SimpleDateFormat(resultDateFormat).format(calendar.getTime());
        //获取当前季度的结束时间
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
        int month1 = calendar1.get(Calendar.MONTH);
        int quarter1 = month1 / 3 + 1;
        int endMonth = 3;
        if (quarter1 == 2){
            endMonth = 6;
        }else if(quarter1 == 3){
            endMonth = 9;
        }else if(quarter1 == 4){
            endMonth = 12;
        }
        calendar1.set(Calendar.MONTH,endMonth - 1);
        int lastDay = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar1.set(Calendar.DAY_OF_MONTH,lastDay);
        String date2=new SimpleDateFormat(resultDateFormat).format(calendar1.getTime());
        return date1+"_"+date2;
    }

    /**
     * 获取指定日期所属年的第一天和最后一天,用下划线连接
     * @param dataStr
     * @param dateFormat
     * @param resultDateFormat
     * @return
     * @throws ParseException
     */
    public static String getFirstAndLastOfYear(String dataStr, String dateFormat, String resultDateFormat) throws ParseException {
        //获取当年的第一天
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
        final int last = calendar.getActualMinimum(Calendar.DAY_OF_YEAR);
        calendar.set(Calendar.DAY_OF_YEAR, last);
        String date1=new SimpleDateFormat(resultDateFormat).format(calendar.getTime());
        //获取当年的左后一天
        Calendar calendar1 = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat(dateFormat).parse(dataStr));
        final int last1 = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
        calendar1.set(Calendar.DAY_OF_YEAR, last1);
        String date2=new SimpleDateFormat(resultDateFormat).format(calendar1.getTime());
        return date1+"_"+date2;
    }
}
